package com.simtop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.simtop.aop.SystemServiceLog;
import com.simtop.domain.simtopnews.News;
import com.simtop.domain.simtopnews.NewsVo;
import com.simtop.exception.CustomException;
import com.simtop.mapper.NewsMapper;
import com.simtop.service.NewsService;
import com.simtop.utils.FileUploadAndDownload;
import com.simtop.utils.ifnull.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lvyongwei
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    /**
     * 添加新闻内容
     * @param news
     * @return
     */
    @Override
    @SystemServiceLog(description = "添加新闻内容")
    public boolean insertNews(News news,MultipartFile photo, List<MultipartFile> files) {
        if(news.getCreateTime() == null){
            news.setCreateTime(new Date());
        }
        if(photo!=null && photo.getOriginalFilename().length()!=0){
            String photoPath = fileUploadAndDownload.upload2(photo, "news");
            if(StringUtils.isEmpty(photoPath)){
                throw new CustomException("封面图片上传失败");
            }
            news.setNewsPhoto(photoPath);
        }
        if(files.size() > 0){
            List<JSONObject> lists = new ArrayList<JSONObject>();
            for(MultipartFile multipartFile:files){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name",multipartFile.getOriginalFilename());
                String photoPath = fileUploadAndDownload.upload2(multipartFile, "news");
                jsonObject.put("path",photoPath);
                lists.add(jsonObject);
            }
            if(lists.size() == 0){
                throw new CustomException("附件内容上传失败");
            }
            news.setNewsAccessory(JSONObject.toJSONString(lists));
        }
        return newsMapper.insertNews(news);
    }

    /**
     * 获取新闻列表
     * @return
     */
    @Override
    public List<NewsVo> getAllNews(News news) {
        return newsMapper.getAllNews(news);
    }

    /**
     * 更新新闻操作
     * @param news
     * @return
     */
    @Override
    @SystemServiceLog(description = "更新新闻操作")
    public boolean updateNews(News news,MultipartFile photo, List<MultipartFile> files) {
        if(photo != null && photo.getOriginalFilename().length()!=0){
            String photoPath = fileUploadAndDownload.upload2(photo, "news");
            if(StringUtils.isEmpty(photoPath)){
                throw new CustomException("封面图片上传失败");
            }
            news.setNewsPhoto(photoPath);
        }
        if(files.size() > 0){
            List<JSONObject> lists = new ArrayList<JSONObject>();
            for(MultipartFile multipartFile:files){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name",multipartFile.getOriginalFilename());
                String photoPath = fileUploadAndDownload.upload2(multipartFile, "news");
                jsonObject.put("path",photoPath);
                lists.add(jsonObject);
            }
            if(lists.size() == 0){
                throw new CustomException("附件内容上传失败");
            }
            news.setNewsAccessory(JSONObject.toJSONString(lists));
        }
        return newsMapper.updateNews(news);
    }

    /**
     * 删除新闻
     * @param ids
     * @return
     */
    @Override
    @SystemServiceLog(description = "删除新闻操作")
    public boolean deleteNews(List<Long> ids) {
        return newsMapper.deleteNews(ids);
    }

    /**
     * 根据id获取新闻详情
     * @param newsId
     * @return
     */
    @Override
    public News getNewsById(Long newsId) {
        return newsMapper.getNewsById(newsId);
    }
}
